<?php

class StatusController extends Asd_Controller
{

    public function init()
    {
        parent::init();
        $this->view->baseUrl = $this->_request->getBaseUrl();
    }

    public function indexAction()
    {
        
        $this->view->title = 'Системанын абалы';
        //$tasks = new Application_Model_Status();
        //$this->view->tasks = $tasks->getTasks();
        $db = Zend_Db_Table::getDefaultAdapter(); 
        
                $select = $db->select()
                    ->from(array('a' => 'status'))
                    ->join(array('b' => 'users'), 'a.user_id = b.user_id', array('user_name' => 'user_name', 'user_surname' => 'user_surname'))
                    ->join(array('c' => 'problems'), 'a.problem_id = c.problem_id', array('problem_title' => 'title'))
                    //->join(array('d' => 'lang_prog'), 'a.lang_prog_id = d.lang_prog_id', array('lang_prog_title' => 'title'))
                    ->joinLeft(array('f' => 'tests_result'), 'a.source_id = f.source_id', array(new Zend_Db_Expr('MAX(f.test_num) AS test'), new Zend_Db_Expr('MAX(f.result_id) AS result_id'), new Zend_Db_Expr('MAX(f.time_limit) AS time1'), new Zend_Db_Expr('MAX(f.memory_limit) AS memory1')))
                    ->joinLeft(array('problem_source'), 'problem_source.source_id = a.source_id', array())
                    ->join(array('d' => 'lang_prog'), 'problem_source.lang_prog_id = d.lang_prog_id', array('d.lang_prog_id'))
                    ->group('a.status_id')
                    ->order('a.date DESC');
                
        //echo $select->__toString(); exit();
        $paginator = new Zend_Paginator(new Zend_Paginator_Adapter_DbSelect($select));
        $paginator->setCurrentPageNumber($this->_getParam('page'));
        $paginator->setItemCountPerPage(10);
        $this->view->paginator = $paginator;
    }

    public function sourceAction()
    {
        $id = $this->_getParam('id');
        $this->view->title = 'Исходники';
        $source = new Application_Model_Source();
        $task_source = $source->getSource($id);
        $this->view->source = $task_source;
        
        $testResult = new Application_Model_TestsResult();
        $this->view->testResult = $testResult->getTestResult($id);
    }
    
    public function ratingAction() {
        
        $this->view->title = 'Рейтинг';
        $db = Zend_Db_Table::getDefaultAdapter();
        
        $paginator = new Zend_Paginator(new Zend_Paginator_Adapter_DbSelect($db->select()
                ->from('users', array('user_name', 'user_surname', 'user_from'))
                ->joinLeft('problem_source', 'result_id = 1 and users.user_id = problem_source.user_id' , array(new Zend_Db_Expr('COUNT(DISTINCT problem_id) AS count')))
                ->group('problem_source.user_id')
                ->order(new Zend_Db_Expr('COUNT(DISTINCT problem_source.problem_id) desc'))));
        
        
        $paginator->setCurrentPageNumber($this->_getParam('page'));
        $paginator->setItemCountPerPage(30);
        $this->view->paginator = $paginator;
    }

}

